package io.plus.user_interface.io.server.command.command_handler;

import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.interactive.Io;
import io.plus.user_interface.io.common.io;
import io.plus.user_interface.io.common.packet.Command;
import io.plus.user_interface.io.common.packet.HandshakeRequestMessage;
import io.plus.user_interface.io.common.packet.ResponseMessage;
import io.plus.user_interface.io.common.protocol.PacketImpl;
import io.plus.user_interface.io.common.protocol.ProtocolConstants;
import io.plus.user_interface.io.common.utils.ImKit;
import io.plus.user_interface.io.server.command.AbstractCommandHandler;

/**
 * 心跳cmd命令处理器
 */
public class HandshakeReqHandler extends AbstractCommandHandler {

    @Override
    public Command command() {
        return Command.handshake_request;
    }

    @Override
    public PacketImpl handler(PacketImpl packet, AsynchronousSocketChannelContext asynchronousSocketChannelContext) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(Command.handshake_response, new HandshakeRequestMessage(ProtocolConstants.HANDSHAKE_BYTE));
        PacketImpl handshakePacket = ImKit.convertResponsepPacket(responseMessage, asynchronousSocketChannelContext);

        if (handshakePacket == null) {
            Io.remove(asynchronousSocketChannelContext, "业务层不同意握手");
            return null;
        }
        io.send(asynchronousSocketChannelContext, handshakePacket);


        return null;
    }
}
